package com.neo.controller;

import com.neo.bean.*;
import com.neo.common.base.Base2Controller;
import com.neo.mapper.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.*;


import java.util.Map;

@Api(value = "Api接口", description = "Api接口Api接口", tags = "program")
@Slf4j
@RestController
@RequestMapping("/api")
public class IndexIController extends Base2Controller {

    @Autowired
    private ProvinceMapper provinceMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private CountryMapper countryMapper;
    @Autowired
    private StreetMapper streetMapper;
    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private AreaMapper areaMapper;

    @GetMapping(value = "/test")
    @ApiOperation(value = "查询所有数据", notes = "/test")
    @ResponseBody
    public Map<String, Object> test() {
        List<Provinces> provinces = provinceMapper.selectAll();
        for (Provinces province : provinces) {
            City city = new City();
            city.setProvinceId(province.getId());
            List<City> cities = cityMapper.select(city);
            String provinceAreaIds = "";
            for (City city1 : cities) {
                provinceAreaIds += city1.getId() + ",";
                Country country = new Country();
                country.setCityId(city1.getId());
                List<Country> countries = countryMapper.select(country);
                String cityAreaIds = "";
                for (Country country1 : countries) {
                    cityAreaIds += country1.getId() + ",";
                    Street street = new Street();
                    street.setCountryId(country1.getId());
                    List<Street> streets = streetMapper.select(street);
                    String countryAreaIds = "";
                    for (Street street1 : streets) {
                        countryAreaIds += street1.getId() + ",";
                        add(street1.getId(), "", street1.getName(), country1.getId(), 3, street1.getId());
                        adds(street1.getId(), street1.getCode(), street1.getHref(), street1.getName(), street1.getCountryId(), 3);
                    }
                    add(country1.getId(), countryAreaIds, country1.getName(), city1.getId(), 2, country1.getId());
                    adds(country1.getId(), country1.getCode(), country1.getHref(), country1.getName(), country1.getCityId(), 2);
                }
                add(city1.getId(), cityAreaIds, city1.getName(), province.getId(), 1, city1.getId());
                adds(city1.getId(), city1.getCode(), city1.getHref(), city1.getName(), city1.getProvinceId(), 1);
            }
            add(province.getId(), provinceAreaIds, province.getName(), 0, 0, province.getId());
            adds(province.getId(), province.getCode(), province.getHref(), province.getName(), 0, 0);
        }
        return sucess();
    }

    public void add(Integer id, String areaIds, String name, Integer parentId, int level, int organizationId) {
        Organization organization = new Organization();
        organization.setAddressId(id);
        organization.setId(organizationId);
        organization.setAreaIds(areaIds);
        organization.setStatus(0);
        organization.setCreateDate(System.currentTimeMillis() + "");
        organization.setUpdateDate(System.currentTimeMillis() + "");
        organization.setName(name + "市场监督管理局");
        organization.setLevel(level);
        organization.setParentId(parentId);
        organizationMapper.insert(organization);
    }

    public void adds(Integer id, String code, String areaIds, String name, Integer parentId, int level) {
        Area organization = new Area();
        organization.setId(id);
        organization.setName(name);
        organization.setParentId(parentId);
        organization.setCode(code);
        organization.setHref(areaIds + "");
        organization.setLevel(level);
        areaMapper.insert(organization);
    }
}
